<template>
	<view class="container">
		<!-- 商品列表 -->
		<scroll-view 
			scroll-y 
			class="product-list"
			@scrolltolower="loadMore"
			refresher-enabled
			:refresher-triggered="isRefreshing"
			@refresherrefresh="onRefresh"
		>
			<view 
				class="product-item" 
				v-for="(item, index) in productList" 
				:key="index"
				@click="navigateToDetail(item.id)"
			>
				<image :src="item.image" mode="aspectFill" class="product-img"></image>
				<view class="product-info">
					<text class="name">{{item.name}}</text>
					<text class="desc">{{item.description}}</text>
					<view class="bottom">
						<text class="price">¥{{item.price}}</text>
						<text class="sales">已售{{item.sales}}</text>
					</view>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="loading-more" v-if="hasMore">
				<text>加载中...</text>
			</view>
			<view class="no-more" v-else>
				<text>没有更多了</text>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="productList.length === 0">
				<image src="/static/images/empty.png" mode="aspectFit" class="empty-img"></image>
				<text class="empty-text">暂无商品</text>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 推荐类型
const type = ref('')

// 是否还有更多数据
const hasMore = ref(true)

// 是否正在刷新
const isRefreshing = ref(false)

// 商品列表
const productList = ref([])

// 页面标题
const pageTitle = ref('')

// 加载商品列表
const loadProducts = () => {
	// 根据类型加载不同的商品数据
	switch (type.value) {
		case 'guess':
			productList.value = [
				{
					id: '10002',
					name: '麻辣牛肉',
					description: '精选牛肉，麻辣鲜香',
					price: '68.00',
					sales: '800',
					image: 'https://img.alicdn.com/imgextra/i2/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
				},
				{
					id: '10003',
					name: '毛肚',
					description: '新鲜毛肚，爽脆可口',
					price: '88.00',
					sales: '600',
					image: 'https://img.alicdn.com/imgextra/i3/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
				},
				{
					id: '10004',
					name: '虾滑',
					description: '鲜虾制作，Q弹爽滑',
					price: '58.00',
					sales: '500',
					image: 'https://img.alicdn.com/imgextra/i4/O1CN01XCiY1B1nx2Xh2Z9tP_!!6000000005155-0-tps-1316-736.jpg'
				}
			]
			break
		case 'related':
			productList.value = [
				{
					id: '10005',
					name: '特色花椒油',
					description: '精选花椒，香气浓郁',
					price: '29.90',
					sales: '856',
					image: 'https://img.alicdn.com/imgextra/i2/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
				},
				{
					id: '10006',
					name: '秘制蘸料组合',
					description: '多种口味，随心搭配',
					price: '35.80',
					sales: '2341',
					image: 'https://img.alicdn.com/imgextra/i3/O1CN01Z5paLt1goYDdHEzSU_!!6000000004150-0-tps-1316-736.jpg'
				},
				{
					id: '10007',
					name: '特色香油',
					description: '纯芝麻油，香气四溢',
					price: '25.90',
					sales: '1234',
					image: 'https://img.alicdn.com/imgextra/i4/O1CN01XCiY1B1nx2Xh2Z9tP_!!6000000005155-0-tps-1316-736.jpg'
				}
			]
			break
		case 'hot':
			productList.value = [
				{
					id: '10008',
					name: '特色火锅底料',
					description: '正宗重庆味道，麻辣鲜香',
					price: '39.90',
					sales: '1234',
					image: 'https://img.alicdn.com/imgextra/i4/O1CN01XCiY1B1nx2Xh2Z9tP_!!6000000005155-0-tps-1316-736.jpg'
				},
				{
					id: '10009',
					name: '特色蘸料',
					description: '多种口味，随心搭配',
					price: '35.80',
					sales: '2341',
					image: 'https://img.alicdn.com/imgextra/i2/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
				}
			]
			break
	}
}

// 加载更多
const loadMore = () => {
	if (!hasMore.value) return
	
	// 这里可以添加加载更多商品的接口调用
	setTimeout(() => {
		hasMore.value = false
	}, 1000)
}

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true
	// 重置列表并重新加载
	productList.value = []
	hasMore.value = true
	
	setTimeout(() => {
		isRefreshing.value = false
		loadProducts()
	}, 1000)
}

// 跳转到商品详情
const navigateToDetail = (id) => {
	uni.navigateTo({
		url: `/pages/detail/view?id=${id}`
	})
}

onMounted(() => {
	// 获取推荐类型
	const pages = getCurrentPages()
	const currentPage = pages[pages.length - 1]
	type.value = currentPage.options.type
	
	// 设置页面标题
	switch (type.value) {
		case 'guess':
			pageTitle.value = '猜你喜欢'
			break
		case 'related':
			pageTitle.value = '相关推荐'
			break
		case 'hot':
			pageTitle.value = '热销商品'
			break
	}
	
	// 设置导航栏标题
	uni.setNavigationBarTitle({
		title: pageTitle.value
	})
	
	// 加载商品列表
	loadProducts()
})
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #F5F5F5;
}

/* 商品列表样式 */
.product-list {
	height: 100vh;
}

.product-item {
	display: flex;
	background-color: #FFFFFF;
	padding: 30rpx;
	margin-bottom: 20rpx;
}

.product-img {
	width: 200rpx;
	height: 200rpx;
	border-radius: 10rpx;
	margin-right: 20rpx;
}

.product-info {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.name {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 10rpx;
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}

.desc {
	font-size: 24rpx;
	color: #999;
	margin-bottom: 10rpx;
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}

.bottom {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.price {
	font-size: 32rpx;
	color: #D02B29;
	font-weight: bold;
}

.sales {
	font-size: 24rpx;
	color: #999;
}

/* 加载更多样式 */
.loading-more, .no-more {
	text-align: center;
	padding: 20rpx 0;
	font-size: 24rpx;
	color: #999;
}

/* 空状态样式 */
.empty-state {
	padding: 100rpx 0;
	text-align: center;
}

.empty-img {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
}
</style> 